package org.flycloud.web.decoupling;

import java.io.IOException;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpRequest {

	private String point;

	public String getPoint() {
		return point;
	}

	public void setPoint(String point) {
		this.point = point;
	}

	private CloseableHttpClient httpCilent = HttpClients.createDefault();

	public String get(String url) {
		System.out.println("req:" + point + url);
		HttpGet httpGet = new HttpGet(point + url);
		try {
			CloseableHttpResponse httpResponse = httpCilent.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String srtResult = EntityUtils.toString(httpResponse.getEntity());
				return srtResult;
			} else if (httpResponse.getStatusLine().getStatusCode() == 400) {
				return null;
			} else if (httpResponse.getStatusLine().getStatusCode() == 500) {
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void close() {
		try {
			httpCilent.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
